
const { ccclass, property } = cc._decorator;
import baseManager from "../../../app/base/baseManager";

@ccclass
export default class subManager extends baseManager {
    private last = null;
    private curr = null;
    private currCanvasSizeType = 0;

    static EventType = cc.Enum({
        "SHOWGAMEOVERRANK": "SHOWGAMEOVERRANK",
        "SHOWFRIENDRANK": "SHOWFRIENDRANK",
        "SHOWGROUPRANK": "SHOWGROUPRANK",
        "SHOWSURPASS": "SHOWSURPASS",

        "HIDEGAMEOVERRANK": "HIDEGAMEOVERRANK",
        "HIDEFRIENDRANK": "HIDEFRIENDRANK",
        "HIDEGROUPRANK": "HIDEGROUPRANK",
        "HIDESURPASS": "HIDESURPASS"
    })

    private setCanvasSize(type) {
        if ((<any>window).wx && (<any>window).sharedCanvas) {
            if (this.currCanvasSizeType === type) {
                return;
            }
            this.currCanvasSizeType = type;
            switch (type) {
                case 1:
                    if (cc.sys.os.toUpperCase() === "IOS") {
                        (<any>window).sharedCanvas.width = 320;
                        (<any>window).sharedCanvas.height = 450;
                    } else {
                        (<any>window).sharedCanvas.width = 150;
                        (<any>window).sharedCanvas.height = 211;
                    }
                    break;
                case 2:
                    if (cc.sys.os.toUpperCase() === "IOS") {
                        (<any>window).sharedCanvas.width = 640 * 2;
                        (<any>window).sharedCanvas.height = 900 * 2;
                    } else {
                        (<any>window).sharedCanvas.width = 640 * 0.7;
                        (<any>window).sharedCanvas.height = 900 * 0.7;
                    }
                    break;
                case 3:
                    if (cc.sys.os.toUpperCase() === "IOS") {
                        (<any>window).sharedCanvas.width = 640 * 2;
                        (<any>window).sharedCanvas.height = 900 * 2;
                    } else {
                        (<any>window).sharedCanvas.width = 640 * 0.7;
                        (<any>window).sharedCanvas.height = 900 * 0.7;
                    }
                    break;
            }
        }
    }

    private changeShow(name) {
        if ((name === this.curr) || (name === "friendRank" && this.curr === "groupRank") || (this.curr === "friendRank" && name === "groupRank")) {
            this.last = this.last;
        } else {
            this.last = this.curr;
        }
        if (this.curr) {
            this.emit("HIDE" + this.curr.toUpperCase())
        }
        this.curr = name;
        if (this.curr) {
            this.emit("SHOW" + this.curr.toUpperCase())
        }
    }

    public showLast() {
        if (this.last) {
            switch (this.last) {
                case "surpass":
                    this.showSurpass();
                    this.changeShow("surpass");
                    break;
                case "gameOverRank":
                    this.showGameOverRank();
                    this.changeShow("gameOverRank");
                    break;
                case "friendRank":
                    this.showFriendRank();
                    this.changeShow("friendRank");
                    break;
                case "groupRank":
                    this.showGroupRank();
                    this.changeShow("groupRank");
                    break;
            }
        }
    }

    public canShowSurpass() {
        return this.curr === "surpass";
    }

    public showSurpass() {
        this.setCanvasSize(1);
        cc.app.lib.sdk.postMessage({ event: "surpass" });
        this.changeShow("surpass");
    }

    public setSurpassScore(score = 0) {
        cc.app.lib.sdk.postMessage({
            event: "score",
            data: score || 0
        });
    }

    public canShowGameOverRank() {
        return this.curr === "gameOverRank";
    }

    public showGameOverRank() {
        this.setCanvasSize(3);
        cc.app.lib.sdk.postMessage({
            event: "rank",
            data: 2
        });
        this.changeShow("gameOverRank");
    }

    public canShowFriendRank() {
        return this.curr === "friendRank";
    }

    public showFriendRank() {
        this.setCanvasSize(2);
        cc.app.lib.sdk.postMessage({
            event: "rank",
            data: 1
        });
        this.changeShow("friendRank");
    }

    public canShowGroupRank() {
        return this.curr === "groupRank";
    }

    public showGroupRank(shareTicket?: any) {
        this.setCanvasSize(2);
        cc.app.lib.sdk.postMessage({
            event: "groupRank",
            data: shareTicket
        });
        this.changeShow("groupRank");
    }

    /**
     * 保存用户得分
     * @param {*} score 
     */
    public saveUserScore(score = 0) {
        cc.app.lib.sdk.postMessage({
            event: "setUserScore",
            data: score || 0
        });
    }
}